// components/time_block/time_block.js
const util = require('../../utils/util')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    ti_list: {type: Array, value: []},
    offset: {type: Number, value: 0}
  },

  observers: {
    "ti_list": function(val) {
      this.setData({_list: val})
      this.handle()
    },
    "offset": function(val) {
      this.setData({_offset: val})
      this.handle()
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    _list: [],
    _offset: -1,
    windowWidth: 0,
  },

  ready: function() {
    var _this = this;
    wx.getSystemInfo({
        success: function(res) {
            _this.setData({
                windowWidth: res.windowWidth,
            });
        }
    });
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handle: function() {
      // for (const index in this.data._list) {
      //   let item = this.data._list[index]
      //   //打印调试
      //   let test_bt = new Date(parseInt(item.reserveStartTime))
      //   let test_et = new Date(parseInt(item.reserveEndTime))
      //   console.log(util.formatTimeFromStamp(test_bt), util.formatTimeFromStamp(test_et), this.data._offset > 0 ? 'nextday':'today')
      //   //打印调试结束
      // }
      var _l = []
      // this.data._list =[
      //   {reserveStartTime: new Date('2022-11-23 20:07:00').getTime(), reserveEndTime: new Date('2022-11-23 23:07:00').getTime()},
      //   {reserveStartTime: new Date('2022-11-25 10:20:00').getTime(), reserveEndTime: new Date('2022-11-25 11:20:00').getTime()},
      // ]
      for (let i=0; i<24; i++) {
        _l.push(this.check(i))
      }
      this.setData({list: _l})
    },

    check: function(blockId) {
      var today = new Date()
      var today_zero = new Date(new Date().setHours(0, 0, 0, 0))

      if (this.data._offset > 0) {
        today = new Date(today_zero.setDate(today_zero.getDate()+this.data._offset))
      }
      let nt_h = today.getHours()
      let nt_m = today.getMinutes()
      for (const index in this.data._list) {
        let item = this.data._list[index]
        let bt = new Date(parseInt(item.reserveStartTime))
        let bt_h = bt.getHours()
        let bt_m = bt.getMinutes()
        let et = new Date(parseInt(item.reserveEndTime))
        let et_h = et.getHours()
        let et_m = et.getMinutes()

        let bt_0 = new Date(new Date(bt).setHours(0, 0, 0, 0))
        let et_0 = new Date(new Date(et).setHours(0, 0, 0, 0))

        if (bt_0 > today_zero) {
          break
        }
        if (et_0 < today_zero) {
          continue
        }
        if (bt_0 < today_zero) {
          bt_h = 0
          bt_m = 0
        }
        if (et_0 > today_zero) {
          et_h = 23
          et_m = 60
        } 

        let obj = {}
        if (blockId < nt_h) {
          obj.invalid = 100
          obj.subs = []
          return obj
        }

        if (blockId == nt_h) {
          obj.invalid = nt_m/60.0*100
        }
        
        if (blockId > bt_h && blockId < et_h) {
          if (blockId > nt_h) {
            obj.invalid = 0
          }
          obj.subs = [{status: 1, percent: 100}]
          return obj
        }

        if (blockId == et_h) {
          //检查和下一个开始时间是否为同一小时
          if (this.data._list.length > parseInt(index)+1) {
            let next = this.data._list[parseInt(index)+1]
            let nbt = new Date(parseInt(next.reserveStartTime))
            let nbt_h = nbt.getHours()
            let nbt_m = nbt.getMinutes()
            if (blockId == nbt_h && today.getDate() == nbt.getDate()) {
              let left =  et_m/60.0*100
              let right = (60-nbt_m)/60.0*100
              obj.subs = [{status: 1, percent: left}, {status: 0, percent: 100-left-right}, {status: 1, percent: right}]
              return obj
            }
          }
          //不是同一小时
          obj.subs = [{status:1, percent: et_m/60.0*100}]
          return obj
        }
        if (blockId == bt_h) {
          let right = (60-bt_m)/60.0*100
          obj.subs = [{status:0, percent: 100-right}, {status: 1, percent: right}]
          return obj
        }
      }

      //无占用数据默认填充
      let obj = {}
      if (blockId < nt_h) {
        obj = {
          invalid: 100,
          subs: [],
        }
      }
      else if (blockId == nt_h) {
        obj = {
          invalid: nt_m/60.0*100,
          subs: [{status:0, percent:100}],
        }
      }
      else {
        obj = {
          invalid: 0,
          subs: [{status:0, percent:100}],
        }
      }
      return obj
    }
  },

})
